function executeCmd(cmd)
    output, status = hs.execute(cmd,true)
    if not status then
        hs.alert.show("命令执行失败!\nCMD:" .. cmd .. "\nERROR:" .. output)
    end
end

function execute(cmd)
    return function()
        executeCmd(cmd)
    end
end

function openWeb(url,browser)
    browser = browser or defaultBrowser
    return function()
        hs.execute(string.format([[osascript -e 'tell application "%s"
        activate
        open location "%s"
    end tell']],browser,url))
    end
end

function openWebList(urls,browser)
    browser = browser or defaultBrowser
    return function()
        local delay = 0.5
        for _,url in pairs(urls) do
            hs.timer.delayed.new(delay,function()
                local cmd = string.format(
                    [[osascript -e 'tell application "%s"
                    activate
                    open location "%s"
                    end tell']],browser,url)
                    hs.execute(cmd)
            end):start()
            delay = delay + 0.5
        end
    end
end

function explore(path)
    return function() 
        executeCmd("open -aFinder "..path)
        executeCmd("echo "..path.." | pbcopy")
    end
end

function openApp(app,param)
    return function()
        local cmd = "open -a \'"..app.."\'"
        if param then
            cmd = cmd .. " \'" .. param .. "\'"
        end
        executeCmd(cmd)
    end
end

function openWithTargetApp(path,app)
    return function() 
        executeCmd(string.format([[open -a'%s' %s]],app,path))
    end
end

function openEdgeApp(app)
    return function() 
        executeCmd("open -a ~/Applications/Edge\\ Apps.localized/".. '"' .. app .. '"')
    end
end

function openWithVscode(path)
    return function()
        executeCmd("open -aVisual\\ Studio\\ Code.app "..path)
    end
end

function openWithSublime(path)
    return function()
        executeCmd("open -aSublime\\ Text.app "..path)
    end
end

function cmdWithInput(cmd,title,default)
    return function()
        local localJsonData = {}
        if default == nil then
            localJsonData = hs.json.read("local.json")
            if localJsonData and localJsonData[title] then
                default = localJsonData[title]
            end
        end

        hs.timer.doAfter(0.1, function()
            local win = hs.window.frontmostWindow()
            if win then
                win:raise()
            end
        end)
        
        local result, input = hs.dialog.textPrompt(title or "输入命令参数", "命令:" .. cmd, "" .. default or "", "OK", "Cancel")
        if result == "OK" then
            localJsonData[title] = input
            print(localJsonData[title])
            hs.json.write(localJsonData,"local.json",true,true)
            executeCmd(cmd ..  " " .. input,true)
        end
        
        -- 防止闭包复用
        default = nil
    end
end

-- 定义一个函数，用于打开终端并切换到当前 Finder 目录
function openTerminalInCurrentFinderDir()
    -- 使用 AppleScript 获取当前 Finder 目录路径
    local script = [[
        tell application "Finder"
            set thePath to POSIX path of (target of front window as alias)
        end tell
        return thePath
    ]]
    
    local bool, currentDir, descriptor = hs.osascript.applescript(script)
    if not bool then
        -- hs.alert.show("获取 Finder 当前目录失败")
        currentDir = "~/"
    end
    print("------>",bool,currentDir,descriptor)
    
    -- 打开终端
    local termApp = hs.application.find("Terminal")
    -- 如果终端应用没有运行，则启动它
    if not termApp then
        hs.application.launchOrFocus("Terminal")
        -- 稍等一下，等待终端启动
        hs.timer.doAfter(0.5, function()
            changeDirInTerminal(currentDir)
        end)
    else
        changeDirInTerminal(currentDir)
    end
end

-- 定义一个函数，用于在终端中切换目录
function changeDirInTerminal(dir)
    -- 构建用于切换目录的命令
    local command = "cd " .. dir
    -- 在新的终端标签页执行命令
    hs.osascript.applescript([[
        tell application "Terminal"
            activate
            do script "]] .. command .. [["
        end tell
    ]])
end